﻿using PLCS.Application.Contracts.ErrorLibraryApp.Dtos;
using PLCS.Application.Contracts.ErrorLogApp.Dtos;
using PLCS.Client.IService.LogManager;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.LogManager;

public class ErrorLogService : BaseService<ErrorLogDto, ErrorLogEditDto>, IErrorLogService
{
    public async Task DiseposeErrorAsync(Guid Id, string reason, string solution)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"errorLog/diseposeError/{Id}?reason={reason}&solution={solution}");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<List<ErrorLogDto>> GetUnDisposedErrorAsync()
    {
        RequestParam requestParam = new(method: Method.Get, route: "errorLog/unDisposedError");
        var apiRes = await restClient.ExecuteAsync<List<ErrorLogDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new List<ErrorLogDto>();
    }

    public async Task<PagedResultDto<ErrorLogDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? code, string? kind, string? source, string? startTime, string? endTime)
    {
        var route = $"errorlog/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (!string.IsNullOrEmpty(code))
            route += $"&code={code}";
        if (!string.IsNullOrEmpty(kind))
            route += $"&kind={kind}";
        if (!string.IsNullOrEmpty(source))
            route += $"&source={source}";
        if (!string.IsNullOrEmpty(startTime))
            route += $"&startTime={startTime}";
        if (!string.IsNullOrEmpty(endTime))
            route += $"&endTime={endTime}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<ErrorLogDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<ErrorLogDto>();
    }

    public async Task DiseposeAllErrorAsync()
    {
        RequestParam requestParam = new(method: Method.Post, route: $"errorLog/diseposeAllError");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }
}